package com.janal.controller;

import com.janal.model.dao.user.User;
import com.janal.model.service.register.RegisterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author Lenovo
 */
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从前端获取用户输入的账号密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if("".equals(username) || "".equals(password)){
            req.setAttribute("error","用户名或密码不能为空！");
            req.getRequestDispatcher("register.jsp").forward(req,resp);
            return;
        }
        User user = new User(username,password);
        //调用service添加用户信息
        RegisterServiceImpl rsi = new RegisterServiceImpl();
        if(rsi.register(user)){
            req.getSession().setAttribute("supersession",user);
            resp.sendRedirect("/s/success.jsp");
        }else {
            req.setAttribute("error","该用户已存在！");
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
